<?php
class ModeleFormationClient extends CI_Model
{
    function __construct()
    {
        // Appel au constructeur parent
        parent::__construct();
    }
	function listeFormations($matricule)
	{
		$this->load->database();
		$this->load->library('pagination');
		$this->load->library('table');
		$config['base_url'] = 'http://localhost/CrmIgniter_FormArmor_versionNovembre/index.php/gestionFormClient/lister';
		$config['total_rows'] = $this->countFormations($matricule);
		$config['per_page'] = '4';
		$config['num_links'] = '3';
		$config['full_tag_open'] = '<p>';
		$config['full_tag_close'] = '</p>';

		$this->pagination->initialize($config);

		$criteres= "(f.libelle = s.libelleform and f.niveau = s.niveauform) and (f.libelle = p.libelle_form and f.niveau = p.niveau) and (s.close = 0) and (s.nb_places > s.nb_inscrits) and p.matricule = '".$matricule."' and numero not in (select num_session from inscription where matricule = '".$matricule."')";
        $this->db->select('numero, libelleform, f.niveau, type, description');
        $this->db->from('formation f, plan_formation p, session_form s');
		$this->db->where($criteres);
		$req = $this->db->get("",$config['per_page'],$this->uri->segment(4));
		return $req;
	}
	function countFormations($matricule)
	{
		$criteres= "(f.libelle = s.libelleform and f.niveau = s.niveauform) and (f.libelle = p.libelle_form and f.niveau = p.niveau) and (s.close = 0) and (s.nb_places > s.nb_inscrits) and p.matricule = '".$matricule."' and numero not in (select num_session from inscription where matricule = '".$matricule."')";
        $this->db->select('numero, libelleform, f.niveau, type, description');
        $this->db->from('formation f, plan_formation p, session_form s');
		$this->db->where($criteres);
		$req = $this->db->get();
		return $req->num_rows();
	}
	function inscriptionFormation($matricule,$num_session,$libelle)
	{
		$this->load->database();
		$this->load->helper('date');
		$dataInsert = array(
			'matricule' => $matricule,
			'num_session' => $num_session,
			'date_inscription ' => date('Y-m-d'));
		$this->db->insert('inscription',$dataInsert);

		$req =  "UPDATE session_form  set `nb_inscrits`= nb_inscrits+1 WHERE numero = ".$num_session;
                $this->db->query($req);

	}
	function getFormation($matricule,$num_session)
	{
		$this->load->database();
		$this->db->select('libelleform, f.niveau, type, description');
		$this->db->from('formation f, plan_formation p, session_form s, inscription i');
		$criteres = "(f.libelle = s.libelleform and f.niveau = s.niveauform) and (f.libelle = p.libelle_form and f.niveau = p.niveau) and (p.matricule = '".$matricule."') and (i.num_session = '".$num_session."')";
		$this->db->where($criteres);
		$req = $this->db->get();
		return $req;
	}
}
?>